package com.example.demo.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.dao.DemoMapper;
import com.example.demo.entity.Demo;
import com.example.demo.service.DemoService;

@Service
public class DemoServiceImpl implements DemoService {
	private static final Logger LOG = LoggerFactory.getLogger(DemoServiceImpl.class);
	@Autowired
	private DemoMapper demoMapper;

	@Override
	public int add(Demo demo) {
		try {
			LOG.info(":::::::::::::::::::::::示例数据插入操作开始");
			int res = demoMapper.insert(demo);
			LOG.info(":::::::::::::::::::::::示例数据插入操作结束,插入数据的主键："+res);
			return res;
		} catch (Exception e) {
			LOG.info(":::::::::::::::::::::::示例数据插入操作异常");
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public boolean remove(long id) {
		try {
			LOG.info(":::::::::::::::::::::::示例数据删除操作开始");
			int res = demoMapper.delete(id);
			LOG.info(":::::::::::::::::::::::示例数据删除操作结束,删除数据的主键："+id);
			return res>0 ? true : false;
		} catch (Exception e) {
			LOG.info(":::::::::::::::::::::::示例数据删除操作异常");
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean modify(Demo demo) {
		try {
			LOG.info(":::::::::::::::::::::::示例数据修改操作开始");
			int res = demoMapper.update(demo);
			LOG.info(":::::::::::::::::::::::示例数据修改操作结束,删除数据的主键："+demo);
			return res>0 ? true : false;
		} catch (Exception e) {
			LOG.info(":::::::::::::::::::::::示例数据修改操作异常");
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Demo queryOne(long id) {
		try {
			LOG.info(":::::::::::::::::::::::示例数据查询操作开始");
			Demo res = demoMapper.selectOne(id);
			LOG.info(":::::::::::::::::::::::示例数据查询操作结束,查询数据："+res);
			return res;
		} catch (Exception e) {
			LOG.info(":::::::::::::::::::::::示例数据查询操作异常,主键：" + id);
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Demo> querys(Demo demo) {
		try {
			LOG.info(":::::::::::::::::::::::示例数据查询操作开始");
			List<Demo> res = demoMapper.selects(demo);
			LOG.info(":::::::::::::::::::::::示例数据查询操作结束,查询数据量："+res.size());
			return res;
		} catch (Exception e) {
			LOG.info(":::::::::::::::::::::::示例数据查询操作异常,查询条件：" + demo);
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据条件统计示例集合
	 * @param demo 条件载体
	 * @return 示例集合
	 */
	public int counts(Demo demo) {
		int res = 0;
		try {
			LOG.info(":::::::::::::::::::::::示例数据统计操作开始");
			res = demoMapper.counts(demo);
			LOG.info(":::::::::::::::::::::::示例数据统计操作结束,统计数据量："+res);
		} catch (Exception e) {
			LOG.info(":::::::::::::::::::::::示例数据统计操作异常,统计条件：" + demo);
			e.printStackTrace();
		}
		return res;
	}

}
